package cn.wzx.service;

import cn.wzx.entity.Book;
import cn.wzx.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {
    @Autowired(required = false)
    BookMapper bookMapper;

    // 书本全查询
    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    // 根据ID查询
    @Override
    public Book findById(Integer id) {
        return bookMapper.findById(id);
    }

    // 分页查询
    @Override
    public List<Book> findByPage(Integer start, Integer num) {
        Map map = new HashMap();
        map.put("start",start*num);
        map.put("num",num);
        return bookMapper.findByPage(map);
    }

    // 查询页数
    @Override
    public int count() {
        return bookMapper.count();
    }

    // 添加书本
    @Override
    public void add(Book book) {
        bookMapper.addBook(book);
    }

    // 修改书本
    @Override
    public void update(Book book) {
        bookMapper.update(book);
    }

    // 动态条件查询书本
    @Override
    public List<Book> findIf(String str, int start, int num) {
        // 用于前端查询关键字变红
        String span = "<span style=\"color:red;\">" + str + "</span>";
        List<Book> lst = bookMapper.findIf(str, start * num, num);
        String s = "";
        for (int i = 0; i < lst.size(); i++) {
            Book b = lst.get(i);
            String[] name = b.getName().split(str);
            s = String.join(span, (CharSequence[])name);
            b.setName(s);
            String[] author = b.getAuthor().split(str);
            s = String.join(span, (CharSequence[])author);
            b.setAuthor(s);
            String[] publisher = b.getPublisher().split(str);
            s = String.join(span, (CharSequence[])publisher);
            b.setPublisher(s);
        }
        return lst;
    }

    // 动态条件查询后的总页数
    @Override
    public int findIfCount(String str) {
        int total = bookMapper.findIfCount(str);
        return total;
    }

    // 根据类型查询的总页数
    @Override
    public int countTypeId(Integer typeid) {
        return bookMapper.countTypeId(typeid);
    }

    // 根据类型分页查询
    @Override
    public List<Book> findByPageTypeId(Integer start, Integer num, Integer typeid) {
        System.out.println("TYPEID=="+typeid);
        Map map = new HashMap();
        map.put("start",start*num);
        map.put("num",num);
        map.put("typeid",typeid);
        return bookMapper.findByPageTypeId(map);
    }

    // 根据书本ID点赞+1
    @Override
    public void addRate(Integer bookid) {
        bookMapper.addRate(bookid);
    }

    // 查询点赞量前五的书
    @Override
    public List<Book> findRate5() {
        return bookMapper.findRate5();
    }

    // 查询销量前五的书
    @Override
    public List<Book> findSale5() {
        return bookMapper.findSale5();
    }

    // 查询添加到购物车前五的书
    @Override
    public List<Book> findFav5() {
        return bookMapper.findFav5();
    }

    // 根据点赞数分页查询
    @Override
    public List<Book> findPageRate(int start, int num, int typeid) {
        return bookMapper.findBookByPageRate(start*num, num, typeid);
    }

    // 根据销量分页查询
    @Override
    public List<Book> findPageSale(int start, int num, int typeid) {
        return bookMapper.findBookByPageSale(start*num, num, typeid);
    }

    // 根据加入到购物车数量分页查询
    @Override
    public List<Book> findPageFav(int start, int num, int typeid) {
        return bookMapper.findBookByPageFav(start*num, num, typeid);
    }

    //添加到购物车数量+1
    @Override
    public void addFav(Integer id, Integer num) {
        bookMapper.addFav(id, num);
    }

    // 销量+1
    @Override
    public void addSale(Integer id, Integer num) {
        bookMapper.addSale(id,num);
    }


}
